
$(function(){
	
	var busitableKey;
	
	//var commentHtml;
	//加载点击事件的相关评论
	// TODO 文档时点击事件
	$("#").on('click',function(){
		
		initCommentListDoc($("#busitableKeyDoc").val());
	});
	
	$("#comment-btn-new-doc").on('click',function(){
		$("#commentDoc").attr("placeholder","新增一级评论");
		$("#commentDoc").val("");
		$("#parentIdDoc").val("");
		$("#topIdDoc").val("");
		$("#comment-form-div-doc").css("display","");//显示
		$("#comment-form-div-top-doc").append($("#comment-form-div-doc"));
		//$("#comment-form-div").siblings().css("display","none");
	});
	
	//评论保存
	$("#comment-btn-save-doc").on('click',function(){
		
		if($("#commentDoc").val()==null||$("#commentDoc").val()==""){
			$("#comment-form-div-doc").css("display","none");//隐藏
			return;
		};
		Otod.ajax({
			url:ctx+"/pm/comment/save.do",
			data:$('#comment-form-doc').serialize(),
			contentType: "application/x-www-form-urlencoded;",
			successFn:function(data){
				$("#commentDoc").attr("placeholder","我也说一句");
				$("#commentDoc").val("");
				$("#comment-form-div-top-doc").append($("#comment-form-div-doc"));
				initCommentListDoc($("#busitableKeyDoc").val());
				
			}
		});
		
	});
	
	var placeholder;
	
	$("#commentDoc").on('click',function(){
		placeholder = $("#commentDoc").attr("placeholder");
		$("#commentDoc").attr("placeholder","");
	});
	
	$("#commentDoc").on('blur',function(){
		$("#commentDoc").attr("placeholder",placeholder);
	});
	
	//评论删除
	$('#commentListDoc').on('click',".deleteComment",function(){
		var commentId = $(this).attr('id');
		Otod.Msg.confirm("确定要删除该评论吗？", "删除评论",function(){
			Otod.ajax({
				url:ctx+"/pm/comment/delete.do",
				data:commentId,
				successFn:function(data){
					initCommentListDoc($("#busitableKeyDoc").val());
				}
			});
		});
		
		
	});
	
	
	//关闭评论输入框
	$("#comment-btn-close-doc").on('click',function(){
		$("#comment-form-div-doc").css("display","none");//隐藏
	});
	
	//显示评论输入框，并为表单属性赋值
	$('#commentListDoc').on('click',".replyComment",function(){
		
		if($(this).attr("replyToEmpNo")==GOBAL_CUST_NO){
			Otod.Msg.info("不能回复自己的评论！");
			return;
		}
		var parentId = $(this).attr('id');
		var topId = $(this).attr('topId');
		$("#parentIdDoc").val(parentId);
		$("#topIdDoc").val(topId == "" ? parentId : topId );
		$("#commentDoc").attr("placeholder","回复："+$(this).attr("replyToEmp"));
		
		$("#reply"+parentId).append($("#comment-form-div-doc"));
		$("#comment-form-div-doc").css("display","");//显示
	});
	
});



//初始化评论列表
function initCommentListDoc(busitableKey){
	Otod.ajax({
		url:ctx+"/pm/comment/getAllAboutComment.do",
		data:{busitableKey:busitableKey},
		contentType: "application/x-www-form-urlencoded;",
		successFn:function(data){
			var commentMtDoc = $('#commentMtDoc').html();
			Mustache.parse(commentMtDoc);
			
			var rendered = Mustache.render(commentMtDoc,{
				data: data,
				isMyselfCmt: function(){
					return this.createEmpNo == GOBAL_CUST_NO;
				},
				isNotMyself: function(){
					return this.createEmpNo != GOBAL_CUST_NO;
				},
				formatDt: function(){
				return function(text,render){
					return DateUtil.dateMinuteFormat(+render(text));		//+render(text)  加号是把string转换为number类型
				};
			}
			});
			$('#commentListDoc').html(rendered);
			
		}
	});
}